package com.enqualcomm.kids.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.enqualcomm.kids.R;

import common.utils.DensityUtil;
import common.utils.PromptUtil;

public class MyEditDialog3 extends Dialog {
	private EditDialogCallBack callBack;
	private String title;
	private String content;
	private InputMethodManager inputMethodManager;
	private EditText myEdit;

	public MyEditDialog3(Context context, String title, EditDialogCallBack callBack, String content) {
		super(context);
		this.callBack = callBack;
		this.title = title;
		this.content = content;
		this.inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
	}

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.dialog_myedit);

		setDialogAttributes();
		initDialogView();
		inputMethodManager.showSoftInput(myEdit, InputMethodManager.SHOW_FORCED);
	}

	public void setDialogAttributes() {
		Window dialogWindow = getWindow();
		dialogWindow.setGravity(Gravity.CENTER);
		WindowManager.LayoutParams lp = dialogWindow.getAttributes();

		DisplayMetrics dm = new DisplayMetrics();
		dialogWindow.getWindowManager().getDefaultDisplay().getMetrics(dm);

		lp.width = dm.widthPixels - DensityUtil.dip2px(getContext(), 30); // 宽度
		// lp.alpha = 0.9f; // 透明度

		dialogWindow.setAttributes(lp);
		getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
	}

	public void initDialogView() {
		//1.初始化标题
		TextView titleText = (TextView) findViewById(R.id.titleText);
		titleText.setText(title);
		
		//2.初始化编辑框
		myEdit = (EditText) findViewById(R.id.myEdit);
		myEdit.setText(content);
		myEdit.setSelection(content.length());

		//3.初始化确定，取消按钮
		findViewById(R.id.cancelBtn).setOnTouchListener(new OnTouchListener() {
			public boolean onTouch(View arg0, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_UP) {
					//隐藏输入法
					inputMethodManager.hideSoftInputFromWindow(myEdit.getWindowToken(), 0);
					dismiss();
				}
				return false;
			}
		});

		findViewById(R.id.sureBtn).setOnTouchListener(new OnTouchListener() {
			public boolean onTouch(View view, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_UP) {
					String str = myEdit.getText().toString().trim();
					if (!"".equals(str)) {
						callBack.inputOver(str,"");
						inputMethodManager.hideSoftInputFromWindow(myEdit.getWindowToken(), 0);
						dismiss();
					}else {
						PromptUtil.toast(getContext(), R.string.input_empty);
					}
				}
				return false;
			}
		});

	}
}
